package com.loop.unclelend.ui.presenter

import com.loop.unclelend.network.ApiService
import com.loop.unclelend.network.ApiService.addCommonParams
import com.loop.unclelend.network.ApiService.addRandomParams
import com.loop.unclelend.network.IHttpCallback
import com.loop.unclelend.pro.ProPresenter
import com.loop.unclelend.ui.view.IView_Message
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/8/19 16:26
 * @Annotation：消息列表 Presenter 实现
 */
class Pre_Message(private val iView: IView_Message): ProPresenter(iView) {

    /**
     * 获取消息列表
     */
    fun requestMessageListAction(){
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.UNCLE_API_COMMON_URL)
        val params = JSONObject().addCommonParams().addRandomParams().apply {
            put("path", ApiService.API_PATH.UNCLE_API_MESSAGE_LIST_PATH)
        }
        doPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val array = data.optJSONArray("obj")
                if (array != null){
                    val list = arrayListOf<JSONObject>()
                    for (i in 0..<array.length()){
                        val item = array.optJSONObject(i)
                        list.add(item)
                    }
                    iView.showUncleMessageList(list)
                }
            }

            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
                iView.hideUncleLoading()
            }
        })
    }
}